﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LS.Models;
using LS.ModelLogicHelpers;
using LS.ActionFilter;

namespace LS.Controllers
{
    public class MyCardController : Controller
    {
        private MemberCardContext db = new MemberCardContext();
        private MyCardLogicHelper logic = new MyCardLogicHelper();

        private CardAdminLogicHelper adminLogic = new CardAdminLogicHelper();//just for demo , client logic can NOT call this logic.
        //
        // GET: /MyCard/

        [AppAuthorize(NeedCheckRole = true,
           ValidRoles = new string[] { "Member", "Admin" })]
        public ActionResult Index()
        {
            //var membercards = db.MemberCards.Include(m => m.RelatedCard).Include(m => m.RelatedMember);
            //return View(membercards.ToList());
            var mycards = logic.GetCardsByUser();
            return View(mycards);
            //return PartialView(mycards);
        }

        [AppAuthorize(NeedCheckRole = true,
        ValidRoles = new string[] { "Member", "Admin" })]
        public ActionResult MyCardList() 
        {
            var mycards = logic.GetCardsByUser();
            return PartialView("MyCardList", mycards);
        }
        //
        // GET: /MyCard/Details/5

        public ActionResult Details(int MemberCardId = 0)
        {
            //MemberCard membercard = db.MemberCards.Find(id);
            //if (membercard == null)
            //{
            //    return HttpNotFound();
            //}
            CardManageModel cmm = logic.GetDetail(MemberCardId);
            return View(cmm);
        }

        //
        // GET: /MyCard/Create

        public ActionResult Create()
        {
            var model = adminLogic.CreateNewCard();
            return PartialView("ModalCreate", model);
            return View(model);
        }

        //
        // POST: /MyCard/Create

        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Create(MemberCard membercard)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.MemberCards.Add(membercard);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    ViewBag.CardId = new SelectList(db.Cards, "Id", "SeiralNumber", membercard.CardId);
        //    ViewBag.MemberId = new SelectList(db.Members, "Id", "PhoneNumber1", membercard.MemberId);
        //    return View(membercard);
        //}
        [HttpPost]
        public ActionResult Create(CardManageModel model)
        {

            if (logic.AddNewCardForMember(model))
            {
                //return RedirectToAction("Index");
                //return Json(new { s = "Success" });
               var newcard=logic.GetMemberCardBySerialNumber(model.Card.SeiralNumber);
               return Json(newcard);
            }
            return PartialView();
            return View();
        }

        //
        // GET: /MyCard/Edit/5

        public ActionResult Edit(int MemberCardId = 0)
        {
            CardManageModel cmm = logic.GetDetail(MemberCardId);
            //return View(cmm);
            return PartialView("ModalEdit", cmm);
            return PartialView(cmm);
        }

        //
        // POST: /MyCard/Edit/5

        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Edit(MemberCard membercard)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(membercard).State = EntityState.Modified;
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    ViewBag.CardId = new SelectList(db.Cards, "Id", "SeiralNumber", membercard.CardId);
        //    ViewBag.MemberId = new SelectList(db.Members, "Id", "PhoneNumber1", membercard.MemberId);
        //    return View(membercard);
        //}

        // POST: /MyCard/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(CardManageModel model)
        {

            logic.Update(model);
            return RedirectToAction("Index");

            //return View(model);
        }


        //
        // GET: /MyCard/Delete/5

        public ActionResult Delete(int id = 0)
        {
            MemberCard membercard = db.MemberCards.Find(id);
            if (membercard == null)
            {
                return HttpNotFound();
            }
            return View(membercard);
        }

        public ActionResult Renew(int MemberCardId = 0)
        {
            logic.RenewCard(MemberCardId);
            CardManageModel cmm = logic.GetDetail(MemberCardId);
            //return View("Edit", cmm);
            return RedirectToAction("Edit", new { MemberCardId = cmm.MemberCardId });
            //return View(cmm);
        }

        //
        // POST: /MyCard/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            MemberCard membercard = db.MemberCards.Find(id);
            db.MemberCards.Remove(membercard);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}